“The goal is to turn data into information, and information into insight”
– Carly Fiorina, CEO de HP desde 1999 hasta 2005
if(!require("pacman")) install.packages("pacman")
p_load("tidyverse", "maps", "lubridate", "plotly")Usted en la Tarea 1 (pregunta 1.5) tuvo que identificar el porcentaje de películas estrenadas por década. A partir del data frame obtenido, construya un pie chart usando la librería plotly. (20 puntos)
movie_dataset <- read.csv("Datasets/movie_dataset.csv")
# porcentaje de peliculas por decada
decade_movies <- movie_dataset %>%
na.omit() %>%
mutate(year = year(release_date), decade = year - year%%10, n = n()) %>%
group_by(decade, n) %>%
summarise(n()) %>%
mutate(percentage = `n()`/n) %>%
mutate(label = paste0("Década ",decade))p <- plot_ly(decade_movies, labels = ~ label, values = ~percentage, type = "pie",
textposition = "inside",
textinfo = "percent",
hoverinfo = 'text',
text = ~ paste('%', round(percentage*100, 2)),
marker = list(colors = colors,
line = list(color = '#FFFFFF', width = 1))) %>%
layout(title = "Porcentaje de películas por década",
xaxis = list(showgrid = FALSE, zeroline = FALSE, showticklabels = FALSE),
yaxis = list(showgrid = FALSE, zeroline = FALSE, showticklabels = FALSE))
pConsiderando la Tarea 1 (pregunta 1.6), indentifique el presupuesto promedio (budget) por estado y presentelo esta vez utilizando el mapa de los Estados Unidos. (20 puntos)
# cargamos la lista y la transformas a character
lista_estados <- read.csv("Datasets/estados.csv") %>%
as.matrix() %>%
as.character()# Promedio de presupuesto por estado de Estados Unidos
keywords <- read.csv("Datasets/keywords.csv")
peliculas_estado <- keywords %>%
filter(keywords %in% lista_estados) %>%
right_join(movie_dataset, by = "id") %>%
group_by(keywords) %>%
summarise(count=n(), mean(budget)) %>%
mutate(nombre_estado=keywords) %>%
right_join(
tibble(nombre_estado=lista_estados),
by="nombre_estado"
) %>%
select(nombre_estado, count, `mean(budget)`) %>%
mutate(
count=ifelse(is.na(count),0,count)
) %>%
rename("presupuesto" = `mean(budget)`) %>%
arrange(desc(count)) g1 <- ggplot(peliculas_estado) +
geom_map(aes(map_id = nombre_estado, fill = presupuesto/1000000), map = mapa_estados, color = "white") +
expand_limits(x = mapa_estados$long, y = mapa_estados$lat) +
labs(title ="Presupuesto promedio por Estados", subtitle = "Estados Unidos") + xlab("") + ylab("") +
theme_bw() + coord_map("albers", lat0=39, lat1=45) +
scale_fill_gradientn("Budget ($MM)", colours = rev(rainbow(4))) +
geom_text(data = state_name, aes(label = as.character(region), x = long, y = lat), size = 2.5)
g1A partir de las variables seleccionadas del World Development Indicators de su Tarea 2, represente en un mapa los valores que asume cada variable en 1990 y 2015.
if(!require("pacman")) install.packages("pacman")
p_load("tidyverse", "WDI", "gridExtra", "grid", "scales", "maps", "ggthemes", "gganimate")#"NY.GDP.PCAP.PP.CD" -> PIB PPP
#"BN.CAB.XOKA.GD.ZS" -> balance cuenta corriente como porcentaje del pib
wdi_data <- as.tibble(
WDI(indicator = c("NY.GDP.PCAP.PP.CD", "BN.CAB.XOKA.GD.ZS"),
country = "all",
extra = TRUE,
start = 1990,
end = 2015)
)#"NY.GDP.PCAP.PP.CD" -> PIB PPP
#"BN.CAB.XOKA.GD.ZS" -> balance cuenta corriente como porcentaje del pib
wdi_data <- wdi_data %>%
select(country:region) %>%
dplyr::rename(pib = NY.GDP.PCAP.PP.CD, cuenta_corriente = BN.CAB.XOKA.GD.ZS) %>%
dplyr::filter( year %in% c("1990", "2015") & region != "Aggregates") %>%
na.omit() %>%
group_by(country, year) %>%
arrange(year)wdi_data_pib <- wdi_data %>%
select(pib,year,country) %>%
rename(region = country)
wdi_data_cc <- wdi_data %>%
select(cuenta_corriente,year,country) %>%
rename(region = country) world_map <- map_data("world") %>%
left_join(wdi_data_pib, by = "region")
world_map_animate <- world_map %>%
select(long, lat, group, year, pib, region) %>%
arrange(year)
init <- tibble(
year = 1990,
pib = 0, long = 0, lat = 0
)
fin <- tibble(
year = 2015,
pib = 0, long = 0, lat = 0
)
map_animate <- world +
geom_polygon(data = world_map_animate,
aes(x = long, y = lat, group = group, fill = pib/1000,
frame = year,
cumulative = FALSE),
alpha = 0.5) +
geom_polygon(data = init,
aes(x = long, y = lat, fill = pib,
frame = year,
cumulative = FALSE),
alpha = 0) +
geom_polygon(data = fin,
aes(x = long, y = lat, fill = pib,
frame = year,
cumulative = FALSE),
alpha = 0) +
labs(title = "Producto Interno Bruto, ") +
labs(fill = 'PIB (k)') +
labs(caption = "Mapa por Gabriel Cabrera, @Gabo_Cg\nFuente: World Development Indicators") +
theme(plot.title = element_text(hjust = 0.5, vjust = 0.05, size=25)) +
theme(plot.caption = element_text(hjust = 0, color="gray40", size=15)) +
theme(legend.position = c(.5, -.025),
legend.direction = "horizontal",
legend.title.align = 0,
legend.key.size = unit(1.3, "cm"),
legend.title=element_text(size=17),
legend.text=element_text(size=13))
gganimate(map_animate, interval = 0.2, title_frame =T, ani.width=1600, ani.height=820, dpi=800, "pib_1990_2015.gif")Mapa Producto Interno Bruto
wdi_data_pib <- wdi_data %>%
select(pib,year,country) %>%
rename(region = country)
wdi_data_cc <- wdi_data %>%
select(cuenta_corriente,year,country) %>%
rename(region = country) world_map <- map_data("world") %>%
left_join(wdi_data_cc, by = "region")
world_map_animate <- world_map %>%
select(long, lat, group, year, cuenta_corriente, region) %>%
arrange(year)
init <- tibble(
year = 1990,
cuenta_corriente = 0, long = 0, lat = 0
)
fin <- tibble(
year = 2015,
cuenta_corriente = 0, long = 0, lat = 0
)
map_animate <- world +
geom_polygon(data = world_map_animate,
aes(x = long, y = lat, group = group, fill = cuenta_corriente,
frame = year,
cumulative = FALSE),
alpha = 0.5) +
geom_polygon(data = init,
aes(x = long, y = lat, fill = cuenta_corriente,
frame = year,
cumulative = FALSE),
alpha = 0) +
geom_polygon(data = fin,
aes(x = long, y = lat, fill = cuenta_corriente,
frame = year,
cumulative = FALSE),
alpha = 0) +
labs(title = "Cuenta Corriente, ") +
labs(fill = 'CC') +
labs(caption = "Mapa por Gabriel Cabrera, @Gabo_Cg\nFuente: World Development Indicators") +
theme(plot.title = element_text(hjust = 0.5, vjust = 0.05, size=25)) +
theme(plot.caption = element_text(hjust = 0, color="gray40", size=15)) +
theme(legend.position = c(.5, -.025),
legend.direction = "horizontal",
legend.title.align = 0,
legend.key.size = unit(1.3, "cm"),
legend.title=element_text(size=17),
legend.text=element_text(size=13))
gganimate(map_animate, interval = 0.2, ani.width = 1500, ani.height = 750, "CC_1990_2015.gif")Mapa Cuenta Corriente
Visite el sitio web http://datos.gob.cl/ y descargue una base de datos de su preferencia1, justificando su elección. Luego, represente los datos seleccionados en un mapa de una o varias de las comunas de la región Metropolitana. Los mapas vectoriales pueden ser descargados de https://www.bcn.cl.
Debe contener las comunas de la región Metropolitana.↩